var newsTable;

//获取广告信息
function getAdvertising(liveId) {
    if (typeof newsTable !== 'undefined') {
        newsTable.fnPageChange('first');
        return;
    }
    
    var i = 1;
    newsTable = $("#newsList").DataTable({
        "sAjaxSource": _path + "/communication/queryByPage",
        "fnServerParams": function (aoData) {
            aoData.push(
                { 
                	"name": "name",
                	"value": $("#name").val() 
                },{
					"name" : "liveName",
					"value" : $("#liveName").val()
				},{
					"name" : "fromDate",
					"value" : $("#fromDate").val()
				}, {
					"name" : "toDate",
					"value" : $("#toDate").val()
				},{
					"name" : "liveId",
					"value" : liveId
				},{
					
				}
            );
        },
        "sServerMethod": "POST",
        "sAjaxDataProp": "dataList",
        "fnCreatedRow": function () {
            i++;
        },
        "fnPreDrawCallback": function () {
            i = 1;
        },
        "aoColumns": [
            { "sTitle": "序号", "sWidth": "2%",
                "mData": function () {
                    return i;
                }
            },
            { "sTitle": "广告名称", "sWidth": "10%" ,"mData": 
	            	function(data){
	        		var temps = '<a href="' + _path + '/communication/edit/' + data.id + '" >'+data.name+'</a>';
	        		return temps;
	        	}
	        },
	        { "sTitle": "直播间名称", "mData": "live.name", "sWidth": "10%" },
            //{ "sTitle": "图片", "mData": "imgUrl", "sWidth": "10%" },
//            { "sTitle": "链接", "mData": "url", "sWidth": "10%" },
            { "sTitle": "广告类别", "mData": "advertisingType", "sWidth": "10%" ,
            	 "mRender": function (data) {
                     var advertisingType = '';
                     switch (data) {
                         case '1':
                        	 advertisingType = '合作广告';
                             break;
                         case '2':
                        	 advertisingType = '交流广告';
                             break;
                         case '3':
                        	 advertisingType = '合作广告2';
                             break;
                     }
                     return advertisingType;
                 }
            
            },
            { "sTitle": "操作", "sWidth": "10%",
                "mData": function (data) {
                		var status = '<a href="' + _path + '/communication/edit/' + data.id + '" class="btn default btn-xs purple"><i class="fa fa-edit" title="编辑"></i></a>';
                		status += '&nbsp;<a href="javascript:check('+ data.id+ ');"  class="btn default btn-xs red"><i class="fa fa-trash-o" title="删除"></i></a>';
                        return status;
                }
            }
        ]
    });
}

function check(id){
	layer.confirm('您确定要删除吗？', function(){
		layer.closeAll('dialog');
		$.ajax({
			url :  _path + "/communication/del/" + id,
			type : "get",
			dataType : "json",
			async : false,
			error : function(request) {
				alert("连接错误！");
			},
			success : function(data) {
				var liveId=$("#liveId").val();
				  if(data.msg=='成功'){
					  layer.close
					  App.bubble(200, "删除成功");
					
					  getAdvertising(liveId);
				  }else{
					  App.bubble(200, "删除异常");
				  }
			}
		});
		}, function(){
		
		});
}

function openzs(id){
	
	  $.ajax({
          url: _path + '/zsk/listin?lid='+id,
          type: 'GET',
          datatype: "html",//"xml", "html", "script", "json", "jsonp", "text".
          success: function(resp) {
        	  $("#his").html(resp);
        	  var $modal = $("#his");
        	  newsTable1=undefined;
              $modal.modal('show');
          }
      });
	
	
	
}
